package com.njupt.Array;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/2/24 14:30
 * @Description: 73. 矩阵置零
 * @Version: 1.0
 */


public class SetZeroes_73 {


    public void setZeroes(int[][] matrix) {
        boolean[][] isVisited = new boolean[matrix.length][matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (!isVisited[i][j]) {
                    if (matrix[i][j] == 0) {
                        //遍历第i行的元素
                        for (int k = 0; k < matrix[i].length; k++) {
                            if (matrix[i][k] != 0) {
                                matrix[i][k] = 0;
                                isVisited[i][k] = true;
                            }

                        }
                        //遍历第j列的元素
                        for (int k = 0; k < matrix.length; k++) {
                            if (matrix[k][j] != 0) {
                                matrix[k][j] = 0;
                                isVisited[k][j] = true;
                            }

                        }
                    }
                }

            }
        }
    }

    public static void main(String[] args) {
        SetZeroes_73 test = new SetZeroes_73();
//        int[][] matrix = {{1,1,1},{1,0,1},{1,1,1}};
        int[][] matrix = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
        test.setZeroes(matrix);
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length - 1; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println(matrix[i][matrix[0].length - 1]);
        }

    }
}
